QuickOPC User's Guide and Reference
Writing value, timestamp and quality (OPC Classic)
View with Navigation Tools
Development Models > Imperative Programming Model > Imperative Programming Model for OPC Data (Classic and UA) > Modifying Information (OPC Data) > Writing to OPC Classic Items > Writing value, timestamp and quality (OPC Classic)
In This Topic

Some newer OPC servers allow a combination of value, timestamp, and quality (VTQ) be written into their items.

If you need to do this, call WriteItem or WriteMultipleItems method.

A single item

.NET

// This example shows how to write a value, timestamp and quality into a single item.

using System;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.OperationModel;

namespace DocExamples.DataAccess._EasyDAClient
{
    class WriteItem
    {
        public static void Main1()
        {
            // Instantiate the client object.
            var client = new EasyDAClient();

            try
            {
                client.WriteItem("", "OPCLabs.KitServer.2", "Simulation.Register_I4", 
                    12345, DateTime.SpecifyKind(new DateTime(1980, 1, 1), DateTimeKind.Utc), 192);
            }
            catch (OpcException opcException)
            {
                Console.WriteLine($"*** Failure: {opcException.GetBaseException().Message}");
                return;
            }
            Console.WriteLine("Success");
        }
    }
}

COM

// This example shows how to write a value, timestamp and quality into a single item.

class procedure WriteItem.Main;
var
  Client: OpcLabs_EasyOpcClassic_TLB._EasyDAClient;
begin
  // Instantiate the client object
  Client := CoEasyDAClient.Create;

  try
    Client.WriteItem('', 'OPCLabs.KitServer.2', 'Simulation.Register_I4', 12345, EncodeDate(1980, 1, 1), DAQualities_GoodNonSpecific);
  except
    on E: EOleException do
    begin
      WriteLn(Format('*** Failure: %s', [E.GetBaseException.Message]));
      Exit;
    end;
  end;

end;

 

Multiple items

.NET

// This example shows how to write values, timestamps and qualities into 3 items at once.

using System;
using System.Diagnostics;
using OpcLabs.BaseLib.OperationModel;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.OperationModel;

namespace DocExamples.DataAccess._EasyDAClient
{
    class WriteMultipleItems
    {
        public static void Main1()
        {
            // Instantiate the client object.
            var client = new EasyDAClient();

            Console.WriteLine("Writing multiple items...");
            OperationResult[] resultArray = client.WriteMultipleItems(
                new[] { 
                    new DAItemVtqArguments("OPCLabs.KitServer.2", "Simulation.Register_I4", 
                        new DAVtq(23456, DateTime.UtcNow, DAQualities.GoodNonspecific)),
                    new DAItemVtqArguments("OPCLabs.KitServer.2", "Simulation.Register_R8", 
                        new DAVtq(2.34567890, DateTime.UtcNow, DAQualities.GoodNonspecific)),
                    new DAItemVtqArguments("OPCLabs.KitServer.2", "Simulation.Register_BSTR", 
                        new DAVtq("ABC", DateTime.UtcNow, DAQualities.GoodNonspecific))
                });
            
            for (int i = 0; i < resultArray.Length; i++)
            {
                Debug.Assert(resultArray[i] != null);
                if (resultArray[i].Succeeded)
                    Console.WriteLine("Result {0}: success", i);
                else
                {
                    Debug.Assert(!(resultArray[i].Exception is null));
                    Console.WriteLine("Result {0} *** Failure: {1}", i, resultArray[i].ErrorMessageBrief);
                }
            }
        }
    }
}

COM

// This example shows how to write values, timestamps and qualities into 3 items at once.

class procedure WriteMultipleItems.Main;
var
  Arguments: OleVariant;
  Client: OpcLabs_EasyOpcClassic_TLB._EasyDAClient;
  I: Cardinal;
  ItemVtqArguments1: _DAItemVtqArguments;
  ItemVtqArguments2: _DAItemVtqArguments;
  ItemVtqArguments3: _DAItemVtqArguments;
  Results: OleVariant;
  OperationResult: _OperationResult;

begin
  ItemVtqArguments1 := CoDAItemVtqArguments.Create;
  ItemVtqArguments1.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2';
  ItemVtqArguments1.ItemDescriptor.ItemID := 'Simulation.Register_I4';
  ItemVtqArguments1.Vtq.Value := 23456;
  ItemVtqArguments1.Vtq.TimestampLocal := Now();
  ItemVtqArguments1.Vtq.Quality.NumericalValue := DAQualities_GoodNonSpecific;

  ItemVtqArguments2 := CoDAItemVtqArguments.Create;
  ItemVtqArguments2.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2';
  ItemVtqArguments2.ItemDescriptor.ItemID := 'Simulation.Register_R8';
  ItemVtqArguments2.Vtq.Value := 2.34567890;
  ItemVtqArguments2.Vtq.TimestampLocal := Now();
  ItemVtqArguments2.Vtq.Quality.NumericalValue := DAQualities_GoodNonSpecific;

  ItemVtqArguments3 := CoDAItemVtqArguments.Create;
  ItemVtqArguments3.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2';
  ItemVtqArguments3.ItemDescriptor.ItemID := 'Simulation.Register_BSTR';
  ItemVtqArguments3.Vtq.Value := 'ABC';
  ItemVtqArguments3.Vtq.TimestampLocal := Now();
  ItemVtqArguments3.Vtq.Quality.NumericalValue := DAQualities_GoodNonSpecific;

  Arguments := VarArrayCreate([0, 2], varVariant);
  Arguments[0] := ItemVtqArguments1;
  Arguments[1] := ItemVtqArguments2;
  Arguments[2] := ItemVtqArguments3;

  // Instantiate the client object
  Client := CoEasyDAClient.Create;

  // Modify values of nodes
  TVarData(Results).VType := varArray or varVariant;
  TVarData(Results).VArray := PVarArray(
    Client.WriteMultipleItems(Arguments));

  // Display results
  for I := VarArrayLowBound(Results, 1) to VarArrayHighBound(Results, 1) do
  begin
    OperationResult := IInterface(Results[I]) as _OperationResult;
    if OperationResult.Succeeded then
      WriteLn('Result ', I, ' success')
    else
      WriteLn('Result ', I, ' *** Failure: ', OperationResult.Exception.GetBaseException.Message);
  end;

  VarClear(Results);
  VarClear(Arguments);
end;

 

More...

You can also specify the requested data type when writing the value, timestamp and quality. See Data Type in OPC Classic Write.

 

See Also